package mule.view;

//<editor-fold defaultstate="collapsed" desc="Imports">
import javafx.animation.FadeTransition;
import javafx.animation.FadeTransitionBuilder;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Region;
import javafx.scene.layout.RegionBuilder;
import javafx.util.Duration;
import mule.model.GameModel;
import mule.model.Tile;
import mule.model.enums.GameStage;
import mule.model.enums.MuleOutfit;
//</editor-fold>

/**
 *
 * @author stephen
 */
public class MapTile extends Region{
    GameModel model = GameModel.getInstance();
    private ObjectProperty<Tile> modelTile;
    
    private Region highlight = RegionBuilder.create()
        .opacity(0)
        .style("-fx-border-width: 3; -fx-border-color: dodgerblue")
    .build();

    private FadeTransition highlightTransition = FadeTransitionBuilder.create()
        .node( highlight )
        .duration( Duration.millis( 200 ) )
        .fromValue( 0 )
        .toValue( 1 )
    .build();

    public MapTile( ObjectProperty<Tile> tile )
    {
        final ImageView view = new ImageView( tile.get().getImage() );
        getChildren().add( view );
        getChildren().add( highlight );
        modelTile = tile;

        addEventHandler(MouseEvent.MOUSE_ENTERED_TARGET, new EventHandler<MouseEvent>()
        {
            public void handle( MouseEvent t )
            {
                if( modelTile.get().canPurchase() )
                {
                    highlight.setStyle( "-fx-border-width: 3; -fx-border-color: " + model.getTurn().colorString() );
                    highlightTransition.setRate( 1 );
                    highlightTransition.play();
                }
            }
        });
        
        addEventHandler(MouseEvent.MOUSE_EXITED_TARGET, new EventHandler<MouseEvent>()
        {
            public void handle( MouseEvent t )
            {
                if( modelTile.get().canPurchase() )
                {
                    highlight.setStyle( "-fx-border-width: 3; -fx-border-color: " + model.getTurn().colorString() );
                    highlightTransition.setRate( -1 );
                    highlightTransition.play();
                }
            }
        });

        if( !modelTile.get().getName().equals( "town" ) )
        {
            addEventHandler( MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>()
            {
                public void handle( MouseEvent t )
                {
                    if( modelTile.get().canPurchase() && model.getGameStage().equals( GameStage.PropertySelection ) )
                    {
                        highlight.setStyle( "-fx-border-width: 3; -fx-border-color: " + model.getTurn().colorString() );
                        modelTile.get().setCanPurchase( false );
                        model.getTurn().addTileToPlayer( modelTile.get() );
                        if( model.getTurn().getNumberOfOwnedTiles() > 2 )
                        {
                            model.getTurn().setMoney( model.getTurn().getMoney() - 100 );
                        }
                        getChildren().get( 1 ).opacityProperty().set( 1 );
                        if( model.getNextPlayer() == model.players.get( 0 ) )
                        {
                            model.setGameStage( model.getNextGameStage() );
                        }

                        model.setTurn( model.getNextPlayer() );
                    }
                    else if( !modelTile.get().canPurchase() && model.getGameStage().equals( GameStage.Developement ) )
                    {
                        if( model.getTurn().ownedTiles.contains( modelTile.get() ) && 
                            model.getTurn().getMule() != null && 
                            model.getTurn().getMule().getOutfit() != null )
                        {
                            System.out.println( "Tile type: " + modelTile.get().toString() );
                            System.out.println( "Player has mule: " + model.getTurn().getMule() );
                            System.out.println( "Mule has outfit: " + model.getTurn().getMule().getOutfit().toString() );
                            // LAW OF DEMETER. PUT LOGIC INTO MODEL AND AVOID CHAINED CALLS TODO
                            modelTile.getValue().setMule( model.getTurn().getMule() );
                            if( modelTile.getValue().getMuleOutfit().equals( MuleOutfit.FOOD_FACTORY ) )
                            {
                                modelTile.get().activateFoodProduction();
                                System.out.println( getChildren().get( 0 ));
                                getChildren().set( 0, new ImageView( modelTile.get().getFoodImage() ) );
                                System.out.println( getChildren().get( 0 ));
                            }
                            else if( modelTile.getValue().getMuleOutfit().equals( MuleOutfit.ENERGY_FACTORY ) )
                            {
                                modelTile.get().activateEnergyProduction();
                                getChildren().set( 0, new ImageView( modelTile.get().getEnergyImage() ) );
                            }
                            else if( modelTile.getValue().getMuleOutfit().equals( MuleOutfit.ORE_FACTORY ) )
                            {
                                modelTile.get().activateOreProduction();
                                getChildren().set( 0, new ImageView( modelTile.get().getOreImage() ) );
                            }
                        }
                        model.getTurn().removeMule();
                    }
                }
            });
        }
    }

    @Override
    protected void layoutChildren()
    {
        layoutInArea( highlight, 0, 0, getWidth(), getHeight(), getBaselineOffset(), HPos.CENTER, VPos.CENTER );
    }


    public ObjectProperty<Tile> modelTileProperty()
    {
        if( modelTile == null )
        {
            modelTile = new SimpleObjectProperty<Tile>( null, "Default" );
        }
        
        return modelTile;
    }

    public Tile getModelTile()
    {
        return modelTileProperty().get();
    }
}
